package fr.beasymptotic.becv.client;

import java.util.HashMap;
import java.util.Set;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BeCV implements EntryPoint {
	private CV cv;
	private DecoratedTabPanel tp;
	private HTML cvTabContent;
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
//		Widget mainView = getMainView(new CompetencesModelMock());
//		// Add the nameField and sendButton to the RootPanel
//		// Use RootPanel.get() to get the entire body element
//		RootPanel.get("easyCVContainer").add(mainView);
		tp = new DecoratedTabPanel();
		
		HorizontalPanel details = new HorizontalPanel();
		details.add(new Hyperlink("Skills", "skills"));
		details.add(new Hyperlink("Experiences", "experiences"));
		details.add(new Hyperlink("Formations", "formations"));
		
		cvTabContent = new HTML("Baz");;
	    tp.add(details, "foo");
	    tp.add(new HTML("Bar"), "bar");
	    tp.add(cvTabContent, "CV");

	    // Show the 'bar' tab initially.
	    tp.selectTab(1);

	    // Add it to the root panel.
	    RootPanel.get().add(tp);

	    String url = "http://localhost:8080/cv.xml";
	    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));

	    try {
	      Request request = builder.sendRequest(null, new RequestCallback() {
	        public void onError(Request request, Throwable exception) {
	           // Couldn't connect to server (could be timeout, SOP violation, etc.)     
	        }

	        public void onResponseReceived(Request request, Response response) {
	          if (200 == response.getStatusCode()) {
	        	  getCV(response.getText());
	          } else {
	        	  tp.add(new Label(), "ERROR");
	          }
	        }       
	      });
	    } catch (RequestException e) {
	    	tp.add(new Label(e.getMessage()), "Could not connect to server");
	    }
	}

	private void getCV(String xmlCVContent) {
		this.cv = CVBuilder.build(xmlCVContent);
		fireCVUpdated();
	}
	
	private void fireCVUpdated() {
		cvTabContent.setHTML(cv.getPersonalDetails().getFirstName());
		tp.add(new Label(), "dsd");
	}
	
	private Widget getMainView(CompetencesModel model) {
		HorizontalPanel hor = new HorizontalPanel();
		hor.setStylePrimaryName("easycv-mainView");
//		hor.setWidth("100%");
//		hor.setHeight("100%");
		hor.add(new Hyperlink("CV", "CV"));
		Widget skillsView = getSkillsView(model.getCompetences());
		DecoratorPanel decorator = new DecoratorPanel();
		decorator.add(skillsView);
		decorator.setWidth("100%");
		hor.add(decorator);
		return hor;
	}
	
	private Widget getSkillsView(HashMap<String, String[]> orderedCompetences) {
		
		Set<String> skillsGroupsName = orderedCompetences.keySet();
		VerticalPanel root = new VerticalPanel();
		root.setStylePrimaryName("easycv-skillGroup");
		
		for(String skillGroupName : skillsGroupsName) {
			String[] skills = orderedCompetences.get(skillGroupName);
			Widget skillsedget = getCompetences(skills);
			
			Label group = new Label(skillGroupName);
			group.setStyleName("easycv-group");
			root.add(group);
			root.add(skillsedget);
		}
		
		return root;
	}
	
	private Widget getCompetences(String[] competences) {
		FlowPanel flow = new FlowPanel();
		
		InlineLabel skill;
		for(String competence : competences) {
			skill = new InlineLabel(competence);
			skill.setStylePrimaryName("easycv-competencelbl");
			flow.add(skill);
		}
		
		return flow;
	}
}

interface CompetencesModel {
	HashMap<String, String[]> getCompetences();
}
class CompetencesModelMock implements CompetencesModel{
	private HashMap<String, String[]> orderedCompetences;
	
	public CompetencesModelMock() {
		orderedCompetences = new HashMap<String, String[]>();
		orderedCompetences.put("IDE", new String[]{"Eclipse", "Netbeans"});
		orderedCompetences.put("Languages", new String[]{"Java", "PHP", "C++"});
		orderedCompetences.put("J2EE", new String[]{"JSP", "Web Services", "EJB"});
	}
	
	public HashMap<String, String[]> getCompetences() {
		return orderedCompetences;
	}
}